#include "stdafx.h"
#include "ConnectionString.h"

#include <windows.h>
#include <comutil.h>

char* CONNECTION_URL = NULL;

INFOBUS_API const char* CONNECTSTRING_FUNC()
{
  DWORD dwSize = 256;
  char szServer[256], szDatabase[256], szUser[256], szPassword[256];

  if (NULL == CONNECTION_URL) {
    ::GetPrivateProfileString("Repository", "Server", "localhost",szServer,dwSize, "InfobusPrototype.ini");
    ::WritePrivateProfileString("Repository", "Server", szServer, "InfobusPrototype.ini");
    
    ::GetPrivateProfileString("Repository", "Database", "demo",szDatabase,dwSize, "InfobusPrototype.ini");
    ::WritePrivateProfileString("Repository", "Database", szDatabase, "InfobusPrototype.ini");

    ::GetPrivateProfileString("Repository", "User", "demo",szUser,dwSize, "InfobusPrototype.ini");
    ::WritePrivateProfileString("Repository", "User", szUser, "InfobusPrototype.ini");

    ::GetPrivateProfileString("Repository", "Password", "demo",szPassword,dwSize, "InfobusPrototype.ini");
    ::WritePrivateProfileString("Repository", "Password", szPassword, "InfobusPrototype.ini");
   
    _bstr_t connstr = _bstr_t("Driver={SQL Native Client};Server="+_bstr_t(szServer)+";Database="+_bstr_t(szDatabase)+"; Uid="+_bstr_t(szUser)+";Pwd="+_bstr_t(szPassword)+";");
    
    CONNECTION_URL = new char[connstr.length()+1];
    memset(CONNECTION_URL, 0, connstr.length()+1);
    strncpy(CONNECTION_URL, connstr, connstr.length());
  }

  return CONNECTION_URL;
}